﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using MySql.Data.MySqlClient;
using CodeRobot.Utility;
using System.Windows.Forms;

namespace CodeRobot.DAL
{
    /// <summary>
    /// Java Service类
    /// </summary>
    public class JavaServiceHelper
    {

        /// <summary>
        /// 根据表名创建Service类
        /// </summary>
        /// <param name="strFilePath">保存路径</param>
        /// <param name="strProjectName">项目名</param>
        /// <param name="strTableName">表名</param>
        public static void CreateServiceClass(string strFilePath,string strProjectName, string strTableName,string strTableComment)
        {
            //读取版权信息
            CodeRobot.Utility.IniFile iniFile = new Utility.IniFile(Application.StartupPath + "\\config.ini");
            string strCompany = iniFile.GetString("COPYRIGHT", "COMPANY", "");
            string strAuthor = iniFile.GetString("COPYRIGHT", "AUTHOR", "");
            string strVersion = iniFile.GetString("COPYRIGHT", "VERSION", "");
            string strCode = iniFile.GetString("COPYRIGHT", "CODE", "");
            string strCreateDate = iniFile.GetString("BASE", "CREATE_DATE", "");

            string strTableNameUpper = CommonHelper.GetTableNameUpper(strTableName);
            
            string strPrimaryKey = CommonHelper.GetPrimaryKey(strTableName);//如：news_id

            string strAllColumnName = CommonHelper.GetAllColumnName(strTableName);
            string strAllColumnNameNotKey = CommonHelper.GetAllColumnNameNotKey(strTableName);

            string strClassName = CommonHelper.GetClassName(strTableName);//类名
            strClassName = CommonHelper.GetTableNameUpper(strClassName);//News
            string strTableNameSpec = CommonHelper.GetTableNameFirstLowerSecondUpper(strClassName);//如：newsTypes
            string strTableNameLower = strTableNameSpec.ToLower();//如：newstypes

            string strNewPath = strFilePath + "\\" + strTableNameLower + "\\service";
            if (!Directory.Exists(strNewPath))
            {
                Directory.CreateDirectory(strNewPath);
            }

            var utf8WithoutBom = new System.Text.UTF8Encoding(false);
            StreamWriter sw = new StreamWriter(strNewPath + "\\" + strClassName + "Service.java", false, utf8WithoutBom);

            sw.WriteLine("/*");
            sw.WriteLine(" * Copyright " + DateTime.Now.Year + " Cloudin.");
            sw.WriteLine(" *");
            sw.WriteLine(" * Licensed under the Apache License, Version 2.0 (the \"License\");");
            sw.WriteLine(" * you may not use this file except in compliance with the License.");
            sw.WriteLine(" * You may obtain a copy of the License at");
            sw.WriteLine(" *");
            sw.WriteLine(" *      http://www.apache.org/licenses/LICENSE-2.0");
            sw.WriteLine(" *");
            sw.WriteLine(" * Unless required by applicable law or agreed to in writing, software");
            sw.WriteLine(" * distributed under the License is distributed on an \"AS IS\" BASIS,");
            sw.WriteLine(" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.");
            sw.WriteLine(" * See the License for the specific language governing permissions and");
            sw.WriteLine(" * limitations under the License.");
            sw.WriteLine(" */");
            sw.WriteLine("package com.toonan." + strTableNameLower + ".service;");
            sw.WriteLine("");
            sw.WriteLine("import com.baomidou.mybatisplus.service.IService;");
            sw.WriteLine("import com.toonan." + strTableNameLower + ".model." + strClassName + ";");
            sw.WriteLine("import java.util.List;");
            sw.WriteLine("import com.toonan.core.matatype.Dto;");
            sw.WriteLine("import com.baomidou.mybatisplus.plugins.Page;");
            sw.WriteLine("");
            sw.WriteLine("/**");
            sw.WriteLine(" * " + strTableComment + " 服务实现类");
            sw.WriteLine(" * @Author Cloudin");
            sw.WriteLine(" * @CreateDate " + Convert.ToDateTime(strCreateDate).ToString("yyyy年M月d日"));
            sw.WriteLine(" * @UpdateDate " + DateTime.Now.ToString("yyyy年M月d日"));
            sw.WriteLine(" * @Version V" + strVersion + "." + strCode);
            sw.WriteLine(" */");
            sw.WriteLine("public interface " + strClassName + "Service extends IService<" + strClassName + ">  {");
            sw.WriteLine("");
            sw.WriteLine("     /**");
            sw.WriteLine("	 * 根据Dto查询并返回数据持久化对象集合");
            sw.WriteLine("	 *");
            sw.WriteLine("	 * @return List<" + strClassName + ">");
            sw.WriteLine("	 */");
            sw.WriteLine("	List<" + strClassName + "> list(Dto pDto);");
            sw.WriteLine("");
            sw.WriteLine("    /**");
            sw.WriteLine("	 * 根据Dto查询并返回分页数据持久化对象集合");
            sw.WriteLine("	 *");
            sw.WriteLine("	 * @return Page<" + strClassName + ">");
            sw.WriteLine("	 */");
            sw.WriteLine("	Page<" + strClassName + "> listPage(Dto pDto);");
            sw.WriteLine("");
            sw.WriteLine("	/**");
            sw.WriteLine("	 * 根据Dto模糊查询并返回数据持久化对象集合(字符型字段模糊匹配，其余字段精确匹配)");
            sw.WriteLine("	 *");
            sw.WriteLine("	 * @return List<" + strClassName + ">");
            sw.WriteLine("	 */");
            sw.WriteLine("	List<" + strClassName + "> like(Dto pDto);");
            sw.WriteLine("");
            sw.WriteLine("	/**");
            sw.WriteLine("	 * 根据Dto模糊查询并返回分页数据持久化对象集合(字符型字段模糊匹配，其余字段精确匹配)");
            sw.WriteLine("	 *");
            sw.WriteLine("	 * @return Page<" + strClassName + ">");
            sw.WriteLine("	 */");
            sw.WriteLine("	Page<" + strClassName + "> likePage(Dto pDto);");
            sw.WriteLine("");
            sw.WriteLine("}");


            sw.Close();

        }

        
    }
}
